#pragma once
#include <sys/epoll.h>
#include <string.h>
#include "Log.hpp"
#include <errno.h>
#include"nocopy.hpp"


class Epoller:public nocopy
{
public:
    Epoller()
    {
        _epfd = epoll_create(128);
        if (_epfd == -1)
        {
            lg(Error, "epoll_create error: %s", strerror(errno));
        }
        else
        {
            lg(Info, "epoll_create success: %d", _epfd);
        }
    }

    int EpollerCtl(int op, int fd, uint32_t event)
    {
        int n = 0;
        if (op == EPOLL_CTL_DEL)
        {
            // 如果是删除就不用在乎struct epoll_event
            n = epoll_ctl(_epfd, op, fd, nullptr);
            if (n != 0)
            {
                lg(Error, "epoll_ctl delete error!");
            }
        }
        else
        {
            struct epoll_event ev;
            ev.events = event;
            ev.data.fd = fd;
            n = epoll_ctl(_epfd, op, fd, &ev);
            if (n != 0)
            {
                lg(Error, "epoll_ctl error!");
            }
        }

        return n;
    }

    int EpollerWait(struct epoll_event revents[], int maxevents,int timeout)
    {
        int n = epoll_wait(_epfd, revents, maxevents, timeout);
        return n;
    }

    ~Epoller()
    {
        if (_epfd >= 0)
            close(_epfd);
    }

private:
    int _epfd;
};
